#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //this->showFullScreen();
    connectstate = false;
    register_page = new Register;
    register_page->hide();
    page1_init();
    connect_client();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::connect_client()
{
    //调用单例模式的套接字对象
    socket = SocketManager::getinstance().socket();

    //连接成功信号返回提示
    connect(socket,&QTcpSocket::connected,this,[&](){
     //   ui->textEdit->append("成功连接到服务器");
        connectstate =true;
    });
    //连接错误提示
    //connect(socket,QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),this,[&](){
     //   ui->textEdit->append(socket->errorString());
    //});
    //连接断开提示
    connect(socket,&QTcpSocket::disconnected,this,[&](){
        connectstate =false;
    });

    //接收数据显示
    connect(socket,&QTcpSocket::readyRead,this,&MainWindow::start);


}
//页面操作
void MainWindow::page1_init()
{
    ui->label->setScaledContents(true);
    ui->label->setPixmap(QPixmap(":/new/logo.png"));

    courier_page = new Courier;
    courier_page->hide();

    consignee_page = new Consignee;
    consignee_page->hide();

    courier_main_page = new Courier_main;
    courier_main_page->hide();

    courier_add_page = new Courier_add;
    courier_add_page->hide();

    consignee_main_page = new Consignee_main;
    consignee_main_page->hide();

    connect(courier_page,&Courier::returnregister,this,&MainWindow::returnRegister);
    connect(register_page,&Register::tocourier,this,&MainWindow::registertocourier);
    connect(register_page,&Register::toMain,this,&MainWindow::rgttomain);
    connect(courier_page,&Courier::rigfinish,this,&MainWindow::couriertomain);
    connect(register_page,&Register::toconsignee,this,&MainWindow::rgttoconsignee);
    connect(consignee_page,&Consignee::returnreg,this,&MainWindow::consigneeToRegister);
}

void MainWindow::start()
{
    sendRig *mes;
    mes = (sendRig*)socket->readAll().data();
    if(mes->type==1)
    {
        //进入快递员主页面
        telephone_number = mes->tele;
        this->hide();
        courier_main_page->show();
    }else if (mes->type == 2) {
        //进入用户主页面
        telephone_number = mes->tele;
        this->hide();
        consignee_main_page->show();
    }else{
        //登录失败
    }
}

//登录逻辑
void MainWindow::on_pushButton_2_clicked()
{
    sendRig sRg;
    sRg.type = 2;
    const char* telenum = ui->lineEdit->text().toUtf8().constData();
    const char* passw = ui->lineEdit_2->text().toUtf8().constData();
    strcpy(sRg.tele,telenum);
    strcpy(sRg.password,passw);
    sRg.IC=0;
    sRg.Idtype=false;
    strcpy(sRg.Account,"0");
    QByteArray senddata;
    senddata.append((char*)&sRg,sizeof (sendRig));
}



void MainWindow::rgttomain()
{
    register_page->hide();
    this->show();
}



void MainWindow::on_pushButton_3_clicked()
{
    this->hide();
    register_page->show();
}

void MainWindow::returnRegister()
{
    courier_page->hide();
    register_page->show();
}

void MainWindow::registertocourier()
{
    register_page->hide();
    courier_page->show();
}

void MainWindow::couriertomain()
{
    courier_page->hide();
    this->show();
}

void MainWindow::rgttoconsignee()
{
    register_page->hide();
    consignee_page->show();
}

void MainWindow::consigneeToRegister()
{
    consignee_page->hide();
    register_page->show();
}

